package com.yuerenjf.yuerenjinfu.http;

import android.content.Context;

import com.yuerenjf.yuerenjinfu.utils.CacheUtils;
import com.yuerenjf.yuerenjinfu.utils.ConstantUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class HttpGetToken {

    public final static long EXPIRE=7*24*60*60*1000;
    public static void getToken(final  Context context){
      long storeTime=  CacheUtils.getLong(context,ConstantUtils.SP_TOKEN_TIME);
        long currentTime=System.currentTimeMillis();
        if(currentTime-storeTime>EXPIRE){
            Map<String, String> map = new HashMap<String, String>();
            Xutils.getInstance().post(context, Constants.URL_GET_TOKEN, map, new Xutils.XCallBack() {
                @Override
                public void onResponse(String result) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONObject object = jsonObject.getJSONObject("data");
                        if (object != null) {
                            String token = object.getString("token");
                            CacheUtils.setString(context, ConstantUtils.SP_TOKEN,token);
                            CacheUtils.setLong(context, ConstantUtils.SP_TOKEN_TIME,System.currentTimeMillis());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailer(int status, String result) {

                }

                @Override
                public void onFinish() {

                }
            });
        }

    }
}
